/**
 * FtpFileTableModel.java
 * @author: liuex
 * 2007-12-24 ����11:14:45
 *
 * note: 
 */
package org.lex.ftp.ui.explorer.file;

import java.util.Locale;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;

import org.lex.catalog.CatalogEvent;
import org.lex.catalog.CatalogListener;
import org.lex.ftp.model.FTPModel;
import org.lex.net.ftp.IFTPFile;


public class FTPFileTableModel extends AbstractTableModel implements CatalogListener {
	public static final IFTPFile[] EMPTY = new IFTPFile[0];
	private static final IFTPFile FAKE = org.lex.net.ftp.FTPFile.FAKE_FILE;
	private static final String[] COLUMN_NAMES = getLocalizedColumnNames();
	private IFTPFile[] contents = null;

	public FTPFileTableModel() {
		super();
		this.contents = EMPTY;
	}

	public IFTPFile getFtpFile(int row) {
		return this.contents[row];
	}

	public void catalogChanged(CatalogEvent e) {
		this.contents = ((FTPModel) e.getSource()).list();
		this.fireTableDataChanged();
	}

	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	@Override
	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override
	public int getRowCount() {
		return this.contents.length;
	}

	@Override
	public Object getValueAt(int row, int column) {
		IFTPFile file = null;
		if (row >= this.contents.length)
			file = FAKE;
		else
			file = this.contents[row];
		switch (column) {
		case 0:
			// name
			return file;
		case 1:
			// size
			return file;
		case 2:
			// date
			return file.getDate();
		default:
			return "";
		}
	}

	// Localized
	private static String[] getLocalizedColumnNames() {
		Locale locale = Locale.getDefault();
		String fileNameHeaderText = UIManager.getString("FileChooser.fileNameHeaderText", locale);
		String fileSizeHeaderText = UIManager.getString("FileChooser.fileSizeHeaderText", locale);
		String fileDateHeaderText = UIManager.getString("FileChooser.fileDateHeaderText", locale);
		// String fileAttrHeaderText = UIManager.getString(
		// "FileChooser.fileAttrHeaderText", locale );
		return new String[] { fileNameHeaderText, fileSizeHeaderText, fileDateHeaderText
		// ,fileAttrHeaderText
		};
	}
}
